package alpha.com.google.code.mechanic.janitor.ui;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;

import alpha.com.google.code.mechanic.janitor.controller.ContextData;

/**
 * The ContextCopyDialog is to let the user copy all the files of a Context
 * to a new one.
 * 
 * Last_edited: Nov 6, 2014
 * 
 * @History
 * 1. Add code to check Context name duplication.
 * @author jingjiejiang
 * @edited Nov 6, 2014
 * 1) add a private field: ContextData ctxData
 * 2) add checking code in the method: modifyText(ModifyEvent e)
 *
 * @author jingjiejiang
 * @since Apr 12, 2014
 */
public class ContextCopyDialog extends Dialog {
  
  //The title of the dialog.
  private static final String CTX_COPY_DIALOG_TITLE = "Copy a Context";
  
  private Text textSrcCtx;
  private String srcCtxName;
  private Text textDstnCtx;
  private String detnCtxName;
  private ContextData ctxData;

  /**
   * Create the dialog.
   * @param parentShell
   */
  public ContextCopyDialog(Shell parentShell, String srcCtxName) {
    super(parentShell);
    
    this.srcCtxName = srcCtxName;
    
    ctxData = new ContextData();
    try {
      ctxData.readContextsFile();
    } catch (Exception e) {
      MessageDialog.openError(getParentShell(), "Couldn't read the "
          + "context.con file.", e.getMessage());
    }
  }
  
  @Override
  protected void configureShell(Shell shell) {
    super.configureShell(shell);
    if (CTX_COPY_DIALOG_TITLE != null) {
      shell.setText(CTX_COPY_DIALOG_TITLE);
    }
  }
  
  @Override
  public void create() {
    super.create();
    getButton(IDialogConstants.OK_ID).setEnabled(false);
  }
  
  
  public String getDetnCtxName() {
    return detnCtxName;
  }

  /**
   * Create contents of the dialog.
   * @param parent
   */
  @Override
  protected Control createDialogArea(Composite parent) {
    Composite container = (Composite) super.createDialogArea(parent);
    
    Composite composite = new Composite(container, SWT.NONE);
    GridData gd_composite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_composite.heightHint = 74;
    gd_composite.widthHint = 451;
    composite.setLayoutData(gd_composite);
    
    createTextSrcCtx(composite);
    
    Label lblSrcContext = new Label(composite, SWT.NONE);
    lblSrcContext.setAlignment(SWT.RIGHT);
    lblSrcContext.setBounds(0, 0, 128, 14);
    lblSrcContext.setText("Source Context:");
    
    Label lblDstnContext = new Label(composite, SWT.NONE);
    lblDstnContext.setAlignment(SWT.RIGHT);
    lblDstnContext.setBounds(0, 45, 128, 14);
    lblDstnContext.setText("Destination Context:");
    
    textDstnCtx = new Text(composite, SWT.BORDER);
    textDstnCtx.setText("Please input a new Context name here");
    textDstnCtx.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        
        String name = textDstnCtx.getText();
        boolean isenable = false;
        
        if(!name.equals("")) {
          if (ctxData.getContextsCollector().isValidCtxName(name)) {
            isenable = true;
          } else {
            MessageDialog.openError(getParentShell(), "Error", "There "
                + "already exist a Context with the same name. Please "
                + "specify another name for the new Context."); 
          }
        }
      
        getButton(IDialogConstants.OK_ID).setEnabled(isenable);
        
      }
    });
    textDstnCtx.setBounds(134, 42, 317, 19);
    //textDstnCxt.setText("Please input a new Context name here");

    return container;
  }
  
  private void createTextSrcCtx (Composite composite) {
    
    textSrcCtx = new Text(composite, SWT.BORDER);
    textSrcCtx.setEditable(false);
    textSrcCtx.setBounds(134, 0, 317, 19);
    textSrcCtx.setText(srcCtxName);
    textSrcCtx.setBackground(new Color(null, 204, 205, 207));
  }

  /**
   * Create contents of the button bar.
   * @param parent
   */
  @Override
  protected void createButtonsForButtonBar(Composite parent) {
    createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
        true);
    createButton(parent, IDialogConstants.CANCEL_ID,
        IDialogConstants.CANCEL_LABEL, false);
  }

  /**
   * Return the initial size of the dialog.
   */
  @Override
  protected Point getInitialSize() {
    return new Point(470, 157);
  }
  
  @Override
  protected void okPressed () {
    
    detnCtxName = textDstnCtx.getText();
    
    super.okPressed();
  }

}
